home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’89 / ATalk Transfer / tester.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-15  |  12.3 KB  |  487 lines  |  [TEXT/MPS ]

  1. // tester for adsp
  2. // ©1989 bet
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <String.h>
  7. #include <Strings.h>
  8. #include <Devices.h>
  9. #include <Files.h>
  10. #include <AppleTalk.h>
  11. #include <Signal.h>
  12. #include <Errors.h>
  13.  
  14. typedef struct xmitHeader {
  15.     short    numFiles;
  16.     long    dfSize;
  17.     long    rfSize;
  18.     FInfo    fInfo;
  19.     char    fName[32];
  20.     } xmitHeader;
  21.     
  22. static sig_atomic_t     flag;
  23. short                    connRefNum,fRefNum;
  24. AddrBlock                aBlock;
  25. AddrBlock                aBlock;
  26. ATPParamBlock            atpBlock;
  27. MPPParamBlock            nbpPB;
  28. Boolean                    server = false;
  29. BDSType                    bds; // block data stuff for ATP responses
  30.  
  31. #define qSize 600
  32. #define dataBufSiz 512
  33.  
  34. void closeATP()
  35. {
  36. ATPParamBlock    atpBlock;
  37. short error;
  38. // close up ATP
  39.     atpBlock.ATPatpSocket = connRefNum;
  40.     if (error = PCloseATPSkt(&atpBlock,false))
  41.         printf("### error %d closing ATP\n",error);
  42. }
  43.  
  44. long countBlocks(dataSize,blockSize)
  45. long dataSize,blockSize;
  46.  
  47. {
  48. // we need one extra block if there is a partial block at the end.
  49.     return(blockSize / dataSize + (blockSize % dataSize ?1:0));
  50. }
  51.  
  52.  
  53. void rmveName()
  54. {
  55. short error;
  56. char ourEName[99];
  57.  
  58. // remove name
  59.     NBPSetEntity(ourEName,"\pBlackHole","\pTalkServer","\p*");
  60.     nbpPB.NBPentityPtr = ourEName;
  61.     nbpPB.NBPinterval = 5;
  62.     nbpPB.NBPcount = 2;
  63.     if (error = PRemoveName(&nbpPB,false))
  64.         printf("### Error %d removing server name.\n",error);
  65. }
  66.  
  67. void exProc()
  68. {
  69. short    error;
  70.  
  71.     if (server)
  72.         {
  73.         rmveName();
  74.         closeATP();
  75.         }
  76.     if (fRefNum)
  77.         {
  78.         printf("frefnum is %d",fRefNum);
  79.         if(error = FSClose(fRefNum))
  80.             printf("### Error %d closing open file in exProc.\n",error);
  81.         }
  82. }
  83.  
  84. void getFileFork(numBlocks)
  85. long            numBlocks;
  86.  
  87. {
  88. char        dataBuf[8][dataBufSiz];
  89. short        error;
  90. long        dataWritten;
  91.  
  92.     for(; numBlocks; numBlocks--)
  93.         {
  94.     // set up bds
  95.     // we set these up but only use first
  96.     //    for (count=0; count<8; count++)
  97.             {
  98.             bds[0].buffSize = dataBufSiz;
  99.             bds[0].dataSize = dataBufSiz;
  100.             bds[0].buffPtr = dataBuf[0];
  101.             }
  102.  
  103.     // set up rest
  104.         atpBlock.ATPatpFlags = 0;
  105.         atpBlock.ATPaddrBlock = aBlock;
  106.         atpBlock.ATPreqLength = 6;
  107.         atpBlock.ATPreqPointer = "Shit!";
  108.         atpBlock.ATPbdsPointer = (Ptr)bds;
  109.         atpBlock.ATPnumOfBuffs = 1;
  110.         atpBlock.ATPtimeOutVal = 0;
  111.         atpBlock.ATPretryCount = 5;
  112.         if (error = PSendRequest(&atpBlock,false))
  113.             {
  114.             printf("### Error %d sending request for data.\n",error);
  115.             exit(error);
  116.             }
  117.         
  118.     // write out the data we just got
  119.         dataWritten = bds[0].dataSize;
  120.         if (error = FSWrite(fRefNum,&dataWritten,bds[0].buffPtr))
  121.             {
  122.             printf("### Error %d writing data for file.\n",error);
  123.             exit(error);
  124.             }
  125.         }
  126.     
  127.     if(error = FSClose(fRefNum))
  128.         {
  129.         printf("### Error %d closing open file on exit.\n",error);
  130.         exit(error);
  131.         }
  132.     fRefNum = 0;
  133. }
  134.         
  135. void putFileFork(numBlocks)
  136. long            numBlocks;
  137.  
  138. {
  139. char        dataBuf[8][dataBufSiz];
  140. short        error;
  141. long        dataRead;
  142.  
  143.     for(; numBlocks; numBlocks--)
  144.         {
  145.     // set up bds
  146.     // we set these up but only use first
  147.     //    for (count=0; count<8; count++)
  148.             {
  149.             bds[0].buffSize = dataBufSiz;
  150.             bds[0].dataSize = dataBufSiz;
  151.             bds[0].buffPtr = dataBuf[0];
  152.             }
  153.  
  154.     // anybody want anything?  (somebody always wants something!)
  155.         atpBlock.ATPatpSocket = connRefNum;
  156.         bds[0].dataSize = atpBlock.ATPreqLength = dataBufSiz;
  157.         atpBlock.ATPreqPointer = dataBuf[0];
  158.         if (error = PGetRequest(&atpBlock,true))
  159.                 {
  160.                 printf("### Error %d during socket request.\n",error);
  161.             // need to make sure other node doesn't hang here.
  162.                 exit(error);
  163.                 }
  164.         while ((error = atpBlock.ATPioResult) > 0);
  165.         if (error)
  166.             {
  167.             printf("### Error %d on return from getBlock GetRequest.\n",error);
  168.             exit(error);
  169.             }
  170.             
  171.  
  172.     // read in some data, try for a full block, don't worry if not
  173.         dataRead = dataBufSiz;
  174.         if (error = FSRead(fRefNum,&dataRead,bds[0].buffPtr))
  175.             if (error == eofErr)
  176.             // if we didn't get a full block, indicate it in the bds structure,
  177.             // and make sure to set the EOM flag if numBlocks != 1
  178.                 bds[0].buffSize = dataRead;
  179.         else
  180.             {
  181.             printf("### Error %d writing data for file.\n",error);
  182.             exit(error);
  183.             }
  184.             
  185.     // set up rest
  186.         atpBlock.ATPatpSocket = connRefNum;
  187.         atpBlock.ATPbdsPointer = (Ptr)bds;
  188.         atpBlock.ATPnumOfBuffs = 1;
  189.         atpBlock.ATPbdsSize = 1;
  190.         if (error = PSendResponse(&atpBlock,false))
  191.             {
  192.             printf("### Error %d sending request for data.\n",error);
  193.             exit(error);
  194.             }
  195.         }
  196.         
  197.     if(error = FSClose(fRefNum))
  198.         {
  199.         printf("### Error %d closing open file on exit.\n",error);
  200.         exit(error);
  201.         }
  202.     fRefNum = 0;
  203. }
  204.         
  205.         
  206.  
  207. main(argc,argv)
  208. short argc;
  209. char *argv[];
  210.  
  211. {
  212. short            error,vRefNum;
  213. long            numBlocks;
  214. char            ourEName[108],*sPtr,*tempPtr;
  215. char            dataBuf[dataBufSiz];
  216. xmitHeader        ourHeader;
  217. char            pfName[255]; // pascal file name buffer for FS calls
  218.  
  219. // check if user asked for server, i.e. no filenames
  220.     if ((ourHeader.numFiles = argc-1) > 0)
  221.         server = true;
  222.  
  223. // set exit proc
  224.     atexit(exProc);
  225.     
  226. // open drivers
  227.     if (error = ATPLoad()) {printf("error %d opening ATP\n",error); exit(error);}
  228.     
  229. // do files setup
  230.     Debugger();
  231.     GetVol(pfName,&vRefNum);
  232.     
  233. // depending on if we are the sender of the file, either advertise that we
  234. // are available, or look for an advertisement and solicit data.
  235.     if (server)
  236.         {
  237.         SysBeep(5);
  238.     // set up ATP responder socket
  239.         aBlock.aNet = 0;
  240.         aBlock.aNode = 0;
  241.         aBlock.aSocket = 0;
  242.         atpBlock.ATPatpSocket = 0;
  243.         atpBlock.ATPaddrBlock = aBlock;
  244.         atpBlock.ATPioCompletion = nil;
  245.         if(error = POpenATPSkt(&atpBlock,false))
  246.             {
  247.             printf("### Error %d opening responder socket.\n",error);
  248.             exit(error);
  249.             }
  250.             
  251.         connRefNum = atpBlock.ATPatpSocket;
  252.         
  253.     // register our name
  254.         NBPSetNTE(ourEName,"\pBlackHole","\pTalkServer","\p*",connRefNum);
  255.         nbpPB.NBPentityPtr = ourEName;
  256.         nbpPB.NBPinterval = 2;
  257.         nbpPB.NBPcount = 20;
  258.         nbpPB.NBPverifyFlag = true;
  259.         if(error = PRegisterName(&nbpPB,false))
  260.             {
  261.             printf("### Error %d registering name.\n",error);
  262.             exit(error);
  263.             }
  264.         
  265.     // we have a taker, issue a GetRequest to find out what he wants
  266.         atpBlock.ATPatpSocket = connRefNum;
  267.         atpBlock.ATPreqLength = dataBufSiz;
  268.         atpBlock.ATPreqPointer = dataBuf;
  269.         if (error = PGetRequest(&atpBlock,true))
  270.             {
  271.             printf("### Error %d during socket request.\n",error);
  272.         // need to make sure other node doesn't hang here.
  273.             exit(error);
  274.             }
  275.         while ((error = atpBlock.ATPioResult) > 0);
  276.         if (error)
  277.             {
  278.             printf("### Error %d after socket request.\n",error);
  279.         // need to make sure other node doesn't hang here.
  280.             exit(error);
  281.             }
  282.  
  283.     // of course he wants files, so start sending them.
  284.     // general procedure is to prepare the header block with
  285.     // the first file's info, send it, then decrement the numFiles
  286.     // field of the xmitHeader record.
  287.         for(;ourHeader.numFiles;ourHeader.numFiles--)
  288.             {
  289.         // set up file names for use
  290.             strcpy(pfName,sPtr = argv[ourHeader.numFiles]);
  291.             c2pstr(pfName);
  292.             printf("Transfering file %s...",sPtr);
  293.             
  294.             if(error = FSOpen(pfName,vRefNum,&fRefNum))
  295.                 {
  296.                 printf("### Error %d opening file %s.\n",error,sPtr);
  297.             // need to make sure other node doesn't hang here.
  298.                 exit(error);
  299.                 }
  300.             if(error = GetEOF(fRefNum,&ourHeader.dfSize))
  301.                 {
  302.                 printf("### Error %d finding data EOF on file %s.\n",error,sPtr);
  303.             // need to make sure other node doesn't hang here.
  304.                 exit(error);
  305.                 }
  306.                 
  307.             if(error = FSClose(fRefNum))
  308.                 {
  309.                 printf("### Error %d closing open file on exit.\n",error);
  310.                 exit(error);
  311.                 }
  312.             fRefNum = 0;
  313.  
  314.             if(error = OpenRF(pfName,vRefNum,&fRefNum))
  315.                 {
  316.                 printf("### Error %d opening file %s.\n",error,sPtr);
  317.             // need to make sure other node doesn't hang here.
  318.                 exit(error);
  319.                 }
  320.             if(error = GetEOF(fRefNum,&ourHeader.rfSize))
  321.                 {
  322.                 printf("### Error %d finding resource EOF on file %s.\n",error,sPtr);
  323.             // need to make sure other node doesn't hang here.
  324.                 exit(error);
  325.                 }
  326.                 
  327.             if(error = FSClose(fRefNum))
  328.                 {
  329.                 printf("### Error %d closing open file on exit.\n",error);
  330.                 exit(error);
  331.                 }
  332.             fRefNum = 0;
  333.  
  334.             if(error = GetFInfo(pfName,vRefNum,&ourHeader.fInfo))
  335.                 {
  336.                 printf("### Error %d getting finder info on file %s.\n",error,sPtr);
  337.             // need to make sure other node doesn't hang here.
  338.                 exit(error);
  339.                 }
  340.             
  341.             if (tempPtr = strrchr(sPtr,':'))
  342.                 strcpy(ourHeader.fName,tempPtr);
  343.             else
  344.                 strcpy(ourHeader.fName,sPtr);
  345.             
  346.         // set up bds
  347.             bds[0].buffSize = sizeof(xmitHeader);
  348.             bds[0].dataSize = sizeof(xmitHeader);
  349.             bds[0].buffPtr = (Ptr)&ourHeader;
  350.  
  351.         // send this response to make sure that the destination drive has space
  352.         // for this file.  If there is no space, try the next one.
  353.             atpBlock.ATPatpSocket = connRefNum;
  354.             atpBlock.ATPbdsPointer = (Ptr)bds;
  355.             atpBlock.ATPnumOfBuffs = 1;
  356.             atpBlock.ATPbdsSize = 1;
  357.             if (error = PSendResponse(&atpBlock,false))
  358.                 {
  359.                 printf("### Error %d extracting name.\n",error);
  360.                 exit(error);
  361.                 }
  362.             
  363.         // start looping requests for data fork, next for res fork in actual tool
  364.         // to send the file we will open the fork we want, and pass the file
  365.         // refNum to a proc to do the actual reading and writing.  This proc will
  366.         // work for both data and resource forks, since the only difference is the
  367.         // refNum.
  368.         
  369.         // first do data fork
  370.         // we need one extra block if there is a partial block at the end.
  371.             numBlocks = countBlocks(dataBufSiz,ourHeader.dfSize);
  372.             if(error = FSOpen(pfName,vRefNum,&fRefNum))
  373.                 {
  374.                 printf("### Error %d opening source file data fork.\n",error);
  375.                 exit(error);
  376.                 }
  377.             
  378.             putFileFork(numBlocks);
  379.         
  380.         // now do resource fork.
  381.         // again, we need one extra block if there is a partial block at the end.
  382.             numBlocks = countBlocks(dataBufSiz,ourHeader.rfSize);
  383.             if(error = OpenRF(pfName,vRefNum,&fRefNum))
  384.                 {
  385.                 printf("### Error %d opening source file data fork.\n",error);
  386.                 exit(error);
  387.                 }
  388.             
  389.             putFileFork(numBlocks);
  390.             
  391.         // tell user were done with that file!
  392.             printf(" Done!\n");
  393.             }
  394.         }
  395.     else
  396.         {
  397.         NBPSetEntity(ourEName,"\p=","\pTalkServer","\p*");
  398.         nbpPB.NBPentityPtr = ourEName;
  399.         nbpPB.NBPinterval = 2;
  400.         nbpPB.NBPcount = 20;
  401.         nbpPB.NBPretBuffPtr = dataBuf;
  402.         nbpPB.NBPretBuffSize = dataBufSiz;
  403.         nbpPB.NBPmaxToGet = 1;
  404.         if (error = PLookupName(&nbpPB,false))
  405.             {
  406.             printf("### Error %d Looking up server.\n",error);
  407.             exit(error);
  408.             }
  409.         if (error = NBPExtract(dataBuf,nbpPB.NBPnumGotten,1,(EntityName *)ourEName,&aBlock))
  410.             {
  411.             printf("### Error %d extracting name.\n",error);
  412.             exit(error);
  413.             }
  414.             
  415.     // found a source for our data, let's request some and see what we get.
  416.     // the contents of our request doesn't matter as much as making the connection.
  417.  
  418.     // set up bds
  419.         bds[0].buffSize = sizeof(xmitHeader);
  420.         bds[0].dataSize = sizeof(xmitHeader);
  421.         bds[0].buffPtr = (Ptr)&ourHeader;
  422.  
  423.     // set up rest
  424.         atpBlock.ATPatpFlags = 0;
  425.         atpBlock.ATPaddrBlock = aBlock;
  426.         atpBlock.ATPreqLength = 6;
  427.         atpBlock.ATPreqPointer = "Shit!";
  428.         atpBlock.ATPbdsPointer = (Ptr)bds;
  429.         atpBlock.ATPnumOfBuffs = 1;
  430.         atpBlock.ATPtimeOutVal = 0;
  431.         atpBlock.ATPretryCount = 5;
  432.         if (error = PSendRequest(&atpBlock,false))
  433.             {
  434.             printf("### Error %d sending request for header.\n",error);
  435.             exit(error);
  436.             }
  437.         
  438.     // now we should issue requests for data fork, and then for resource fork
  439.     // tell user which file we are transferring and create file.
  440.         printf("Transfering file %s...",ourHeader.fName);
  441.         strcpy(pfName,ourHeader.fName);
  442.         c2pstr(pfName);
  443.         if (error = Create(pfName,vRefNum,ourHeader.fInfo.fdCreator,ourHeader.fInfo.fdType))
  444.             {
  445.             printf("### Error %d creating new file.\n",error);
  446.             exit(error);
  447.             }
  448.     
  449.     // to create the file we will open the fork we want, and pass the file
  450.     // refNum to a proc to do the actual reading and writing.  This proc will
  451.     // work for both data and resource forks, since the only difference is the
  452.     // refNum.
  453.     
  454.     // first do data fork
  455.     // we need one extra block if there is a partial block at the end.
  456.         numBlocks = countBlocks(dataBufSiz,ourHeader.dfSize);
  457.         if(error = FSOpen(pfName,vRefNum,&fRefNum))
  458.             {
  459.             printf("### Error %d opening new file data fork.\n",error);
  460.             exit(error);
  461.             }
  462.         
  463.         getFileFork(numBlocks);
  464.     
  465.     // now do resource fork.
  466.     // again, we need one extra block if there is a partial block at the end.
  467.         numBlocks = countBlocks(dataBufSiz,ourHeader.rfSize);
  468.         if(error = OpenRF(pfName,vRefNum,&fRefNum))
  469.             {
  470.             printf("### Error %d opening new file data fork.\n",error);
  471.             exit(error);
  472.             }
  473.         
  474.         getFileFork(numBlocks);
  475.         
  476.     // finally set file attributes to the same that were in the source file.
  477.         if (error = SetFInfo(pfName,vRefNum,&(ourHeader.fInfo)))
  478.             {
  479.             printf("### Error %d setting new file's file info.\n",error);
  480.             exit(error);
  481.             }
  482.             
  483.     // tell user were done with that file!
  484.         printf(" Done!\n");
  485.         }
  486. }
  487.